package pers.mine.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import pers.mine.po.GoodsItem;
import pers.mine.po.TopShow;
import pers.mine.service.inter.TopShowService;
import pers.mine.utils.MineScrew;
import pers.mine.vo.Info;

/**
 * 轮换图 Controller
 * 
 * @author Mine
 * @since 2016年11月28日_上午10:06:23
 */
@Controller
@RequestMapping("/topShow")
public class AdminTopShowController {
	private String pageTitle = "topShow管理";
	private Info indexinfo = new Info();// 放到首页的信息
	String onePath = "admin/topshow/";
	String imgPath = "/assets/topshowimg";
	@Autowired
	TopShowService topShowService;

	/**
	 * 获得topshow列表,并展示到后台商品页
	 */
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show(Model model) {
		List<TopShow> topShowList = null;
		try {
			// 查询
			topShowList = topShowService.findAll();
			// 判断是否可以放入信息
			if (indexinfo.isCanChange()) {
				indexinfo.setType(Info.SUCCESS);
				indexinfo.setMsg("[商品显示]:查询所有TopShow成功...");
			} else {
				indexinfo.setCanChange(true);
			}
		} catch (Exception e) {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[TopShow显示]:查询所有TopShow失败...");
		}
		model.addAttribute("pageTitle", pageTitle);
		model.addAttribute("indexinfo", indexinfo);
		model.addAttribute("topShowList", topShowList);
		return onePath + "topShowPage";
	}

	// 添加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(TopShow topShow, MultipartFile imgFile,
			HttpServletRequest request, Model model) {
		System.out.println(topShow);
		if (topShow == null) {
			System.out.println("信息有误...[这里和数据校验重复了]");
			return "redirect:show";
		}
		String imgInfo = null;
		String path = request.getServletContext().getRealPath(imgPath);
		// 执行上传
		String img = MineScrew.upLoadImgForStringMVC(path, "topshow", imgFile);
		// System.out.println(img);
		if (img != null) {
			topShow.setImg(img);
			imgInfo = "###TopShow图片上传成功!";
		} else {
			imgInfo = "###TopShow图片未指定或上传失败!";
		}
		// 执行添加操作
		if (topShowService.addTopShow(topShow)) {
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[添加TopShow]:完成添加TopShow操作..." + imgInfo);
		} else {
			// 添加失败,所有要...删除图片
			if (img != null) {
				// 尝试删除旧图片
				File x = new File(new File(path), img);
				System.out.println(x.getAbsolutePath());
				x.delete();
				imgInfo = "###已上传图片已尝试删除!";
			}
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[添加TopShow]:添加TopShow失败..." + imgInfo);
		}
		indexinfo.setCanChange(false);
		// 内部转发
		return "redirect:show";
	}

	// 删除
	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id, String imgName,
			HttpServletRequest request, Model model) {
		String imgInfo = null;
		// 删除topSHow
		if (topShowService.delTopShow(id)) {
			// 尝试...删除图片
			if (imgPath != null) {
				// 尝试删除旧图片
				String path = request.getServletContext().getRealPath(imgPath);
				File x = new File(new File(path), imgName.trim());
				System.out.println(x.getAbsolutePath());
				x.delete();
				imgInfo = "###已上传图片已尝试删除!";
			}
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[删除TopShow]:删除TopShow成功..." + imgInfo);
		} else {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[删除TopShow]:删除TopShow失败..." + imgInfo);
		}
		indexinfo.setCanChange(false);

		// 内部转发
		return "redirect:../show";
	}
}
